pcall(require, "lfs")
local log = log
local lfs = lfs
local setmetatable = setmetatable

module"image_map"

local map = {}

local index_function
if lfs then
	local function fill_image_map(s)
		for file in lfs.dir(s) do
			if file ~= ".." and file ~= "." then
				local sfile = s.."/"..file
				local attr = lfs.attributes(sfile)
				if attr and attr.mode == "directory" then
					fill_image_map(sfile)
				elseif attr and attr.mode == "file" then
					map[sfile:lower()] = sfile
				end
			end
		end
	end
	fill_image_map("Interface")
end

setmetatable(_M, {__index = function (self, file)
	local result = map[file:lower()]
	if not result then
		log("EE Unknown Texture path %s", file)
		result = file
	elseif result ~= file then
		log("WW Invalid Texture path %s (should be %s)", file, result)
	end
	self[file] = result
	return result
end})

